package com.message.api.base.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.annotation.SysLog;
import com.base.annotation.Unsecured;
import com.base.helper.BaseController;
import com.base.helper.Result;
import com.message.api.base.entity.AcConfig;
import com.message.api.base.service.AcConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mzl
 * @since 2020-07-09
 */
@Api(tags = "配置")
@RestController
@RequestMapping("/acConfig")
public class AcConfigController extends BaseController {

    private static final Logger          LOG = LoggerFactory.getLogger(AcConfigController.class);
    @Autowired
    private              AcConfigService entityService;

    @SysLog(action = "acConfig:findByPage", value = "配置管理：获取配置列表")
    @ApiOperation(value = "分页查询配置", notes = "分页查询配置")
    @RequestMapping(method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(name = "size", value = "分页大小", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面：从1开始", paramType = "query")})
    public Result<IPage<AcConfig>> findByPage(final AcConfig example, final Page page) {
        IPage<AcConfig> records = entityService.page(example, page);
        return new Result(true, records);
    }

    @SysLog(action = "acConfig:delete", value = "配置管理：删除配置")
    @ApiOperation(value = "删除配置")
    @RequestMapping(value = "{id}", method = {RequestMethod.DELETE})
    @ApiImplicitParam(name = "id", value = "配置ID", required = true, paramType = "path")
    public Result delete(@PathVariable final Integer id) {
        entityService.removeById(id);
        return new Result(true, "成功删除配置", null);
    }

    @Unsecured
    @SysLog(action = "acConfig:one", value = "配置管理：查询配置详细")
    @ApiOperation(value = "查询单个配置")
    @RequestMapping(value = "{id}", method = {RequestMethod.GET})
    @ApiImplicitParam(name = "id", value = "配置ID", required = true, paramType = "path")
    public Result one(@PathVariable final Integer id) {
        AcConfig entity = entityService.getById(id);
        return new Result(true, entity);
    }

    @SysLog(action = "acConfig:add", value = "配置管理：添加配置")
    @ApiOperation(value = "添加配置")
    @RequestMapping(method = {RequestMethod.POST})
    public Result add(@Valid @RequestBody final AcConfig entity) {
        entityService.save(entity);
        return new Result(true, "成功保存配置", null);
    }

    @SysLog(action = "acConfig:update", value = "配置管理：修改配置")
    @ApiOperation(value = "修改配置")
    @RequestMapping(method = {RequestMethod.PUT})
    public Result update(@Valid @RequestBody final AcConfig entity) {
        if (null == entity.getId()) {
            throw new RuntimeException("id不能为空");
        }
        entityService.updateById(entity);
        return new Result(true, "成功修改配置", null);
    }

    @SysLog(action = "acConfig:all", value = "配置管理：全部配置项")
    @ApiOperation(value = "全部配置")
    @RequestMapping(value = "all", method = RequestMethod.GET)
    public Result<List<AcConfig>> all(AcConfig example) {
        List<AcConfig> entitys = entityService.list(example);
        if (null != entitys) {
            return new Result<>(true, entitys);
        }
        return new Result<>(true, Collections.emptyList());
    }

    @SysLog(action = "acConfig:setConfigvalue", value = "配置管理：设置配置状态")
    @ApiOperation(value = "设置配置状态")
    @RequestMapping(value = "{id}/set/{configvalue}", method = {RequestMethod.POST})
    @ApiImplicitParams({@ApiImplicitParam(name = "configvalue", value = "设置配置状态：1启用，0禁用", required = true, paramType
            = "path"
    )})
    public Result<AcConfig> setStatus(@PathVariable(name = "id") final Integer id,
                                      @PathVariable(name = "configvalue") String configvalue) {
        if ("1".equals(configvalue) || "0".equals(configvalue)) {
            AcConfig acConfig = entityService.setConfigvalue(id, configvalue);
            return new Result<>(true, acConfig);
        } else {
            return new Result(false, "设置配置状态失败", "设置参数非法，只能设置为0或1");
        }
    }

    @SysLog(action = "acConfig:getRegisterStauts", value = "配置管理：获取注册状态（启用/禁用）")
    @ApiOperation(value = "获取注册状态（启用/禁用）")
    @RequestMapping(value = "getRegisterStauts", method = {RequestMethod.GET})
    public Result getRegisterStauts() {
        String configvalue = entityService.getRegisterStatus();
        return new Result(true, configvalue);
    }

}

